//
//  BusinessMapNaviViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/16.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_GDMapManager.h"
#import "TT_GDAnnotationView.h"

#import "BusinessMapNaviViewController.h"

@interface BusinessMapNaviViewController ()<
MAMapViewDelegate,
AMapSearchDelegate,
AMapLocationManagerDelegate
>

@property (weak, nonatomic) IBOutlet UIView *mapBGView;

@property (nonatomic, strong) MAMapView *mapView;

@end

@implementation BusinessMapNaviViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    self.title = @"店铺定位";
        
    [self.mapBGView addSubview:self.mapView];
    
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    
    
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = self.model.lat;
    coordinate.longitude = self.model.lng;


    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = coordinate;
    pointAnnotation.title = self.model.sellerName;
    pointAnnotation.subtitle = self.model.sellerAddress;
    pointAnnotation.lockedToScreen = NO;
    
    
    self.mapView.centerCoordinate = coordinate;

    [self.mapView addAnnotation:pointAnnotation];

}

- (IBAction)imAction:(id)sender {
    [[TT_IMContactManager manager] ImContactWithStoreId:self.model.sellerId];
}

- (IBAction)yuyueAction:(id)sender {
    [[BA_BusinessHelper shared] callSubscribeStoreWith:self.model.sellerId];
}

#pragma mark 气泡&&大头针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    
//    WEAKSELF
    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        return nil;;
    }
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        TT_GDAnnotationView *annotationView = (TT_GDAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[TT_GDAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:reuseIndetifier];
        }
        
        annotationView.image = [UIImage imageNamed:@"map_dingwei"];
        annotationView.canShowCallout = NO;
        annotationView.draggable = NO;        //设置标注可以拖动，默认为NO
        

        [annotationView.callOutView.ActionBtn addTarget:self action:@selector(naviAction) forControlEvents:UIControlEventTouchUpInside];
        
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
//        annotationView.centerOffset = CGPointMake(0, -15);
        return annotationView;
    }
    return nil;
}

- (void)mapView:(MAMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view {

}

- (void)mapView:(MAMapView *)mapView didAnnotationViewTapped:(MAAnnotationView *)view {    
}

- (void)naviAction {
    
    CGFloat latitude  = self.model.lat;  // 纬度
    CGFloat longitude = self.model.lng; // 经度
    NSString *address = self.model.sellerAddress; // 送达地址
    
    [Utilities callMapWithLat:latitude lng:longitude AndAddress:address];
}

- (MAMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MAMapView alloc] init];
        ///如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
        _mapView.showsUserLocation = YES;
        _mapView.userTrackingMode = 1;
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
        //设置地图缩放比例，即显示区域
        [_mapView setZoomLevel:15.1 animated:YES];
        _mapView.delegate = self;
        //设置定位精度
        _mapView.desiredAccuracy = kCLLocationAccuracyBest;
        //设置定位距离
        _mapView.distanceFilter = 5.0f;
    }
    return _mapView;;
}

@end
